﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Data;


public partial class Admin_Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtTemp = new DataTable();
            dtTemp.Columns.Add("Address1");
            dtTemp.Columns.Add("Address2");
            dtTemp.Columns.Add("City");
            dtTemp.Columns.Add("Name");
            dtTemp.Columns.Add("PhoneNo");
            dtTemp.Columns.Add("State");
            dtTemp.Columns.Add("ZipCode");
            dtTemp.Columns.Add("WebSite");
            dtTemp.Columns.Add("Email");
            dtTemp.Columns.Add("DealAddressID");
            ViewState["AddressDetails"] = dtTemp;
            DataTable dtHighlight = new DataTable();
            dtHighlight.Columns.Add("Highlights");
            dtHighlight.Columns.Add("HighLightID");
            ViewState["Highlight"] = dtHighlight;
            BindGrid();
            ViewState["DID"] = "0";

        }
    }
    private void BindGrid()
    {
        var deal = new DealesDataContext();
        var query = deal.Select_Deal();
        RadGrid1.DataSource = query;
        RadGrid1.DataBind();
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {

        if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        {
            Label Price = e.Item.FindControl("lblPrice") as Label;
            Label Date = e.Item.FindControl("lblDate") as Label;
            Price.Text = "<b>Regular Price: $</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "RegularPrice")) + "<br/>" + "<b> Sell Price: $</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "SellPrice")) + "<br/>" + "<b>Discount: $</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Discount"));
            Date.Text = "<b>Sell Start Date : </b>" + Convert.ToString(Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "SellStartDate")).ToShortDateString()) + "<br/>" + "<b>Sell End Date: </b>" + Convert.ToString(Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "SellEndDate")).ToShortDateString()) + "<br/><b>Sell Start Time: </b>" + Convert.ToString(Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "SellStartTime")).ToShortTimeString()) + "<br/><b>Sell End Time: </b>" + Convert.ToString(Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "SellEndTime")).ToShortTimeString()) + "<br/><b>Expiration Date: </b>" + Convert.ToString(Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "ExpirationDate")).ToShortDateString());
        }
    }
    protected void dropVendor_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        string VendorID = e.Value.ToString();

        GridEditableItem editItem = (GridEditableItem)((RadComboBox)o).NamingContainer;
        TextBox txtAddress1 = editItem.FindControl("txtAddress1") as TextBox;
        TextBox txtState = editItem.FindControl("txtState") as TextBox;
        TextBox ZipCode = editItem.FindControl("ZipCode") as TextBox;
        TextBox txtWebSite = editItem.FindControl("txtWebSite") as TextBox;
        TextBox txtEmail = editItem.FindControl("txtEmail") as TextBox;
        TextBox txtAddress2 = editItem.FindControl("txtAddress2") as TextBox;
        TextBox txtCity = editItem.FindControl("txtCity") as TextBox;

        var VendorInfo = new DealesDataContext();
        var query1 = VendorInfo.Select_VendorByID(Convert.ToInt32(VendorID));
        foreach (var item in query1)
        {
            txtEmail.Text = item.Email.ToString();
            txtState.Text = item.State.ToString();
            txtWebSite.Text = item.Website.ToString();
            txtAddress2.Text = item.Address2.ToString();
            txtAddress1.Text = item.Address1.ToString();
            ZipCode.Text = item.ZipCode.ToString();
            txtEmail.Text = item.Email.ToString();
            txtCity.Text = item.City.ToString();
        }

    }
    private string GetSelectedItems(ListBox listBox)
    {
        string str = "";
        foreach (ListItem listItem in listBox.Items)
        {
            if (listItem != null)
            {
                if (listItem.Selected == true)
                {

                    str += listItem.Value + ",";

                }
            }
        }
        str = str.Remove(str.Length - 1, 1);
        return str;

    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        try
        {

            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {




                RadComboBox dropCity = e.Item.FindControl("dropCity") as RadComboBox;
                var deal = new DealesDataContext();
                var City = deal.City_Return();
                dropCity.DataSource = City;
                dropCity.DataTextField = "City";
                dropCity.DataValueField = "CityID";
                dropCity.DataBind();




                RadComboBox dropVendor = e.Item.FindControl("dropVendor") as RadComboBox;
                var Vendor = deal.Vendor_Return();
                foreach (var item in Vendor)
                {
                    RadComboBoxItem Raditem = new RadComboBoxItem();
                    Raditem.Text = item.VendorName.ToString();
                    Raditem.Value = item.VendorID.ToString();
                    string Address1 = (string)item.Address1.ToString();
                    string State = (string)item.State.ToString();
                    Raditem.Attributes.Add("VendorName", item.VendorName.ToString());
                    Raditem.Attributes.Add("Address1", Address1.ToString());
                    Raditem.Attributes.Add("State", State.ToString());
                    dropVendor.Items.Add(Raditem);
                    Raditem.DataBind();
                }


                RadComboBox dropCategory = e.Item.FindControl("dropCategory") as RadComboBox;
                var Category = deal.Industry_Return();
                dropCategory.DataTextField = "Industry";
                dropCategory.DataValueField = "IndustryID";
                dropCategory.DataSource = Category;
                dropCategory.DataBind();




                RadDatePicker RadDatePicker1 = e.Item.FindControl("RadDatePicker1") as RadDatePicker;
                RadDatePicker RadDatePicker2 = e.Item.FindControl("RadDatePicker2") as RadDatePicker;
                RadDatePicker RadDatePicker3 = e.Item.FindControl("RadDatePicker3") as RadDatePicker;

                RadDatePicker1.SelectedDate = System.DateTime.Now;
                RadDatePicker2.SelectedDate = System.DateTime.Now;
                RadDatePicker3.SelectedDate = System.DateTime.Now;

                RadGrid RadGrid2 = e.Item.FindControl("RadGrid2") as RadGrid;
                DataTable dtTemp = new DataTable();
                dtTemp = (DataTable)ViewState["AddressDetails"];
                RadGrid2.DataSource = dtTemp;

                RadGrid RadGrid3 = e.Item.FindControl("RadGrid3") as RadGrid;
                DataTable dtTemp2 = new DataTable();
                dtTemp2 = (DataTable)ViewState["Highlight"];
                RadGrid3.DataSource = dtTemp2;


                RadTimePicker RadTimePicker1 = e.Item.FindControl("RadTimePicker1") as RadTimePicker;
                RadTimePicker RadTimePicker2 = e.Item.FindControl("RadTimePicker2") as RadTimePicker;
                RadTimePicker1.TimeView.Interval = new TimeSpan(0, int.Parse("30"), 0);
                RadTimePicker2.TimeView.Interval = new TimeSpan(0, int.Parse("30"), 0);
                RegularExpressionValidator ImgRequire = e.Item.FindControl("ImgRequire") as RegularExpressionValidator;

                RadioButtonList rbtnDealType = e.Item.FindControl("rbtnDealType") as RadioButtonList;
                System.Web.UI.WebControls.Image ImgPreview = e.Item.FindControl("ImgPreview") as System.Web.UI.WebControls.Image;
                var editableItem = ((GridEditableItem)e.Item);
                var DealID1 = (long)editableItem.GetDataKeyValue("DealID");


                if (Convert.ToInt32(ViewState["DID"]) != Convert.ToInt32(DealID1))
                {
                    //    if (ViewState["DID"].ToString() != "0")
                    {
                        DataTable dtTempR = new DataTable();
                        dtTempR.Columns.Add("Address1");
                        dtTempR.Columns.Add("Address2");
                        dtTempR.Columns.Add("City");
                        dtTempR.Columns.Add("State");
                        dtTempR.Columns.Add("PhoneNo");
                        dtTempR.Columns.Add("Name");
                        dtTempR.Columns.Add("ZipCode");
                        dtTempR.Columns.Add("WebSite");
                        dtTempR.Columns.Add("Email");
                        dtTempR.Columns.Add("DealAddressID");
                        ViewState["AddressDetails"] = dtTempR;
                        DataTable dtHighlightTempR = new DataTable();
                        dtHighlightTempR.Columns.Add("Highlights");
                        dtHighlightTempR.Columns.Add("HighLightID");
                        ViewState["Highlight"] = dtHighlightTempR;



                        DataTable dtTempRR = new DataTable();
                        dtTempRR = (DataTable)ViewState["AddressDetails"];
                        dtTemp = (DataTable)ViewState["AddressDetails"];
                        RadGrid2.DataSource = dtTempRR;


                        DataTable dtTempARR = new DataTable();
                        dtTempARR = (DataTable)ViewState["Highlight"];
                        dtTemp2 = (DataTable)ViewState["Highlight"];
                        RadGrid3.DataSource = dtTempARR;

                        ViewState["AddNewRecord"] = null;
                    }
                }



                ViewState["DID"] = (long)editableItem.GetDataKeyValue("DealID");

                var DealID = (long)editableItem.GetDataKeyValue("DealID");
                var deal1 = new DealesDataContext();
                var query = deal1.Select_DealByID(Convert.ToInt32(DealID));

                var DealAddress = deal1.Select_DealAddressByDealIDForAdmin(Convert.ToInt32(DealID));

                if (dtTemp.Rows.Count > 0)
                {
                }
                else
                {
                    if (Session["AddressDelete"] == null)
                    {
                        DataTable dtTemp1 = new DataTable();
                        dtTemp1.Columns.Add("Address1");
                        dtTemp1.Columns.Add("Address2");
                        dtTemp1.Columns.Add("City");
                        dtTemp1.Columns.Add("State");
                        dtTemp1.Columns.Add("ZipCode");
                        dtTemp1.Columns.Add("PhoneNo");
                        dtTemp1.Columns.Add("Name");
                        dtTemp1.Columns.Add("WebSite");
                        dtTemp1.Columns.Add("Email");
                        dtTemp1.Columns.Add("DealAddressID");
                        foreach (var item in DealAddress)
                        {
                            DataRow dr = dtTemp1.NewRow();
                            dtTemp1.Rows.Add(dr);
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["Address1"] = item.Address1.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["Address2"] = item.Address2.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["City"] = item.City.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["State"] = item.State.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["ZipCode"] = item.ZipCode.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["WebSite"] = item.Website.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["Email"] = item.Email.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["Name"] = item.Name.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["PhoneNo"] = item.PhoneNo.ToString();
                            dtTemp1.Rows[dtTemp1.Rows.Count - 1]["DealAddressID"] = item.DealAddressID.ToString();

                        }
                        if (dtTemp1.Rows.Count > 0)
                        {
                            ViewState["AddressDetails"] = dtTemp1;
                            RadGrid2.DataSource = dtTemp1;
                        }
                    }
                    else
                    {
                        Session["AddressDelete"] = null;
                    }

                }

                var DealHighLight = deal1.Select_HighLight(Convert.ToInt32(DealID));

                if (dtTemp2.Rows.Count > 0)
                {
                }
                else
                {
                    if (Session["HighLightDelete"] == null)
                    {
                        DataTable dtTemp4 = new DataTable();
                        dtTemp4.Columns.Add("Highlights");
                        dtTemp4.Columns.Add("HighLightID");
                        foreach (var item in DealHighLight)
                        {

                            DataRow dr = dtTemp4.NewRow();
                            dtTemp4.Rows.Add(dr);
                            dtTemp4.Rows[dtTemp4.Rows.Count - 1]["Highlights"] = item.HighLights.ToString();
                            dtTemp4.Rows[dtTemp4.Rows.Count - 1]["HighLightID"] = item.HighLightID.ToString();


                        }
                        if (dtTemp4.Rows.Count > 0)
                        {
                            ViewState["Highlight"] = dtTemp4;
                            RadGrid3.DataSource = dtTemp4;
                        }
                    }
                    else
                    {
                        Session["HighLightDelete"] = null;
                    
                    }
                }
                foreach (var item in query)
                {
                    RadDatePicker1.SelectedDate = Convert.ToDateTime(item.SellStartDate.ToString());
                    RadDatePicker2.SelectedDate = Convert.ToDateTime(item.SellEndDate.ToString());
                    RadDatePicker3.SelectedDate = Convert.ToDateTime(item.ExpirationDate.ToString());
                    RadTimePicker1.SelectedDate = Convert.ToDateTime(item.SellEndTime.ToString());
                    RadTimePicker2.SelectedDate = Convert.ToDateTime(item.SellStartTime.ToString());
                    ViewState["CityID"] = item.DealCityID.ToString();
                    dropCity.SelectedValue = item.DealCityID.ToString();
                    dropVendor.SelectedValue = item.VendorID.ToString();
                    dropCategory.SelectedValue = item.DealCategoryID.ToString();
                    rbtnDealType.SelectedValue = item.DealTypeID.ToString();
                    ImgPreview.ImageUrl = "ImageDisplay.aspx?DealID=" + item.DealID;


                }
            }
        }
        catch
        {

            ViewState["DID"] = "0";
            if (ViewState["AddNewRecord"] == null)
            {
                DataTable dtTempR = new DataTable();
                dtTempR.Columns.Add("Address1");
                dtTempR.Columns.Add("Address2");
                dtTempR.Columns.Add("City");
                dtTempR.Columns.Add("State");
                dtTempR.Columns.Add("ZipCode");
                dtTempR.Columns.Add("WebSite");
                dtTempR.Columns.Add("Email");
                dtTempR.Columns.Add("PhoneNo");
                dtTempR.Columns.Add("Name");
                dtTempR.Columns.Add("DealAddressID");
                ViewState["AddressDetails"] = dtTempR;
                DataTable dtHighlightTempR = new DataTable();
                dtHighlightTempR.Columns.Add("Highlights");
                dtHighlightTempR.Columns.Add("HighLightID");
                ViewState["Highlight"] = dtHighlightTempR;



                RadGrid RadGrid2 = e.Item.FindControl("RadGrid2") as RadGrid;
                RadGrid RadGrid3 = e.Item.FindControl("RadGrid3") as RadGrid;
                DataTable dtTempRR = new DataTable();
                dtTempRR = (DataTable)ViewState["AddressDetails"];
                RadGrid2.DataSource = dtTempRR;


                DataTable dtTempARR = new DataTable();
                dtTempARR = (DataTable)ViewState["Highlight"];
                RadGrid3.DataSource = dtTempARR;
                ViewState["AddNewRecord"] = "1";
            }

        }
    }
    protected string TrimDescription(string description)
    {
        string des = System.Text.RegularExpressions.Regex.Replace(description, "<[^>]*>", "");
        if (!string.IsNullOrEmpty(des) && des.Length > 50)
        {
            return string.Concat(des.Substring(0, 20), "...");
        }
        return des;
    }
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        var deal = new DealesDataContext();
        var query = deal.Select_Deal();
        RadGrid1.DataSource = query;

    }
    protected void RadGrid3_InsertCommand(object source, GridCommandEventArgs e)
    {
        DataTable dtHighlight = new DataTable();
        dtHighlight = (DataTable)ViewState["Highlight"];
        TextBox txtHighLights = e.Item.FindControl("txtHighLights") as TextBox;
        Label lblError = e.Item.FindControl("lblError") as Label;
        GridEditableItem editItem = (GridEditableItem)((RadGrid)source).NamingContainer;
        RadGrid RadGrid3 = editItem.FindControl("RadGrid3") as RadGrid;
        if (dtHighlight != null)
        {
            DataRow dr = dtHighlight.NewRow();
            dtHighlight.Rows.Add(dr);
            dtHighlight.Rows[dtHighlight.Rows.Count - 1]["Highlights"] = txtHighLights.Text;
            dtHighlight.Rows[dtHighlight.Rows.Count - 1]["HighLightID"] = dtHighlight.Rows.Count.ToString();
            ViewState["Highlight"] = dtHighlight;
            RadGrid3.DataSource = dtHighlight;
            txtHighLights.Text = "";

        }
    }
    protected void RadGrid2_InsertCommand(object source, GridCommandEventArgs e)
    {
        DataTable dtAddress = new DataTable();
        dtAddress = (DataTable)ViewState["AddressDetails"];
        TextBox txtAddress1 = e.Item.FindControl("txtAddress1") as TextBox;
        TextBox txtState = e.Item.FindControl("txtState") as TextBox;
        TextBox ZipCode = e.Item.FindControl("ZipCode") as TextBox;
        TextBox txtWebSite = e.Item.FindControl("txtWebSite") as TextBox;
        TextBox txtEmail = e.Item.FindControl("txtEmail") as TextBox;
        TextBox txtAddress2 = e.Item.FindControl("txtAddress2") as TextBox;
        TextBox txtCity = e.Item.FindControl("txtCity") as TextBox;
        TextBox txtName = e.Item.FindControl("txtName") as TextBox;
        TextBox txtPhoneNo = e.Item.FindControl("txtPhoneNo") as TextBox;


        Label lblError = e.Item.FindControl("lblError") as Label;

        GridEditableItem editItem = (GridEditableItem)((RadGrid)source).NamingContainer;
        RadGrid RadGrid2 = editItem.FindControl("RadGrid2") as RadGrid;
        if (dtAddress != null)
        {
            DataRow dr = dtAddress.NewRow();
            dtAddress.Rows.Add(dr);
            dtAddress.Rows[dtAddress.Rows.Count - 1]["Address1"] = txtAddress1.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["Address2"] = txtAddress2.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["City"] = txtCity.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["State"] = txtState.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["ZipCode"] = ZipCode.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["WebSite"] = txtWebSite.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["Email"] = txtEmail.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["Name"] = txtName.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["PhoneNo"] = txtPhoneNo.Text;
            dtAddress.Rows[dtAddress.Rows.Count - 1]["DealAddressID"] = dtAddress.Rows.Count.ToString();
            ViewState["AddressDetails"] = dtAddress;
            RadGrid2.DataSource = dtAddress;
            txtAddress1.Text = "";
            txtAddress2.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            ZipCode.Text = "";
            txtWebSite.Text = "";
            txtEmail.Text = "";

        }
    }
    protected void RadGrid2_DeleteCommand(object source, GridCommandEventArgs e)
    {


        string DealAddressID = (string)((GridDataItem)e.Item).GetDataKeyValue("DealAddressID");
        DataTable dtAddress = new DataTable();
        dtAddress = (DataTable)ViewState["AddressDetails"];
        for (int i = 0; i < dtAddress.Rows.Count; i++)
        {
            if (dtAddress.Rows[i]["DealAddressID"].ToString() == DealAddressID)
            {
                dtAddress.Rows[i].Delete();
                dtAddress.AcceptChanges();
                break;
            }
        }
        GridEditableItem editItem = (GridEditableItem)((RadGrid)source).NamingContainer;
        RadGrid AddressDetails = editItem.FindControl("RadGrid2") as RadGrid;
        AddressDetails.DataSource = dtAddress;
        AddressDetails.DataBind();
        Session["AddressDelete"] = 1; 
    }
    protected void RadGrid3_DeleteCommand(object source, GridCommandEventArgs e)
    {
        string HighLightID = (string)((GridDataItem)e.Item).GetDataKeyValue("HighLightID");
        DataTable dtHighLight = new DataTable();
        dtHighLight = (DataTable)ViewState["Highlight"];
        for (int i = 0; i < dtHighLight.Rows.Count; i++)
        {
            if (dtHighLight.Rows[i]["HighLightID"].ToString() == HighLightID)
            {
                dtHighLight.Rows[i].Delete();
                dtHighLight.AcceptChanges();
                break;
            }
        }
        GridEditableItem editItem = (GridEditableItem)((RadGrid)source).NamingContainer;
        RadGrid RadGrid3 = editItem.FindControl("RadGrid3") as RadGrid;
        RadGrid3.DataSource = dtHighLight;
        RadGrid3.DataBind();
        Session["HighLightDelete"] = 1; 
    }
    protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
    {

        var DealID = (long)((GridDataItem)e.Item).GetDataKeyValue("DealID");
        var deal = new DealesDataContext();
        var query = deal.Delete_Deal(DealID);
    }
    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {
      //  oAuthExample.oAuthTwitter oAuth = new oAuthExample.oAuthTwitter();
        Label lblError = e.Item.FindControl("lblError") as Label;
        DataTable dtAddress = new DataTable();
        dtAddress = (DataTable)ViewState["AddressDetails"];        
        DataTable dtHighlight = new DataTable();
        dtHighlight = (DataTable)ViewState["Highlight"];       
        FileUpload FileUpload1 = e.Item.FindControl("GraphicImage") as FileUpload;
        Byte[] bytes = new Byte[10];
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            bytes = br.ReadBytes((Int32)fs.Length);

        }



        System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
        RadComboBox dropVendor = e.Item.FindControl("dropVendor") as RadComboBox;
        RadComboBox dropCity = e.Item.FindControl("dropCity") as RadComboBox;
        RadComboBox dropCategory = e.Item.FindControl("dropCategory") as RadComboBox;
        TextBox txtName = e.Item.FindControl("txtName") as TextBox;
        TextBox txtShortDescription = e.Item.FindControl("txtShortDescription") as TextBox;
        TextBox txtMinQty = e.Item.FindControl("txtMinQty") as TextBox;
        TextBox txtMaxQty = e.Item.FindControl("txtMaxQty") as TextBox;
        TextBox txtPercentageCut = e.Item.FindControl("txtPercentageCut") as TextBox;
        TextBox txtSalesPersonPercentageCut = e.Item.FindControl("txtSalesPersonPercentageCut") as TextBox;
        TextBox txtRegularPrice = e.Item.FindControl("txtRegularPrice") as TextBox;
        TextBox txtSellPrice = e.Item.FindControl("txtSellPrice") as TextBox;
        TextBox txtDiscount = e.Item.FindControl("txtDiscount") as TextBox;
        RadDatePicker RadDatePicker1 = e.Item.FindControl("RadDatePicker1") as RadDatePicker;
        RadDatePicker RadDatePicker2 = e.Item.FindControl("RadDatePicker2") as RadDatePicker;
        RadTimePicker RadTimePicker1 = e.Item.FindControl("RadTimePicker1") as RadTimePicker;
        RadTimePicker RadTimePicker2 = e.Item.FindControl("RadTimePicker2") as RadTimePicker;
        RadDatePicker RadDatePicker3 = e.Item.FindControl("RadDatePicker3") as RadDatePicker;
        RadioButtonList rbtnDealType = e.Item.FindControl("rbtnDealType") as RadioButtonList;
        string s = rbtnDealType.SelectedValue;
        TextBox txtAddress1 = e.Item.FindControl("txtAddress1") as TextBox;
        TextBox txtAddress2 = e.Item.FindControl("txtAddress2") as TextBox;
        TextBox txtCity = e.Item.FindControl("txtCity") as TextBox;
        TextBox txtState = e.Item.FindControl("txtState") as TextBox;
        TextBox ZipCode = e.Item.FindControl("ZipCode") as TextBox;
        TextBox txtWebSite = e.Item.FindControl("txtWebSite") as TextBox;
        TextBox txtEmail = e.Item.FindControl("txtEmail") as TextBox;
        RadEditor txtFootPrint = e.Item.FindControl("txtFootPrint") as RadEditor;
        RadEditor txtDescription = e.Item.FindControl("txtDescription") as RadEditor;
        TextBox txtLimitPerPerson = e.Item.FindControl("txtLimitPerPerson") as TextBox;
     //   CheckBox chkTwitter = e.Item.FindControl("chkTwitter") as CheckBox;

        string EndTime = Convert.ToDateTime(RadDatePicker2.SelectedDate).ToShortDateString() + " " + RadTimePicker1.SelectedDate.Value.TimeOfDay.ToString();
        string StartTime = Convert.ToDateTime(RadDatePicker1.SelectedDate).ToShortDateString() + " " + RadTimePicker2.SelectedDate.Value.TimeOfDay.ToString();
        if (txtPercentageCut.Text == "")
            txtPercentageCut.Text = "0.0";
        if (txtSalesPersonPercentageCut.Text == "")
            txtSalesPersonPercentageCut.Text = "0.0";
        int dealID = 0;
        var deal = new DealesDataContext();

        var query = deal.Insert_Deal(Convert.ToInt32(dropVendor.SelectedValue), Convert.ToInt32(dropCity.SelectedValue), Convert.ToInt32(dropCategory.SelectedValue), 1, 1, txtName.Text, txtDescription.Content, false, Convert.ToDouble(txtMinQty.Text), Convert.ToDouble(txtMaxQty.Text), 1, bytes, Convert.ToDouble(txtPercentageCut.Text), Convert.ToDouble(txtRegularPrice.Text), Convert.ToDouble(txtSellPrice.Text), Convert.ToDouble(txtDiscount.Text), 1, Convert.ToDouble(txtSalesPersonPercentageCut.Text), Convert.ToDateTime(RadDatePicker1.SelectedDate), Convert.ToDateTime(StartTime), Convert.ToDateTime(RadDatePicker2.SelectedDate), Convert.ToDateTime(EndTime), Convert.ToDateTime(RadDatePicker3.SelectedDate), Convert.ToInt32(s), txtFootPrint.Content, txtShortDescription.Text, Convert.ToInt32(txtLimitPerPerson.Text));
        foreach (var item in query)
        {
            dealID = Convert.ToInt32(item.DealID.ToString());
        }
        if (dtAddress != null && dtAddress.Rows.Count > 0)
        {
            for (int i = 0; i < dtAddress.Rows.Count; i++)
            {
                var query1 = deal.Insert_DealAddress(dtAddress.Rows[i]["Name"].ToString(),dtAddress.Rows[i]["Address1"].ToString(), dtAddress.Rows[i]["Address2"].ToString(), dtAddress.Rows[i]["City"].ToString(), dtAddress.Rows[i]["State"].ToString(), dtAddress.Rows[i]["ZipCode"].ToString(), dtAddress.Rows[i]["WebSite"].ToString(), dtAddress.Rows[i]["Email"].ToString(),dtAddress.Rows[i]["PhoneNo"].ToString(), dealID);
            }
        }
        if (dtHighlight != null && dtHighlight.Rows.Count > 0)
        {

            for (int i = 0; i < dtHighlight.Rows.Count; i++)
            {
                var query1 = deal.Insert_HighLights(dtHighlight.Rows[i]["Highlights"].ToString(), dealID);
            }
        }
        DataTable dtTemp = new DataTable();
        dtTemp.Columns.Add("Address1");
        dtTemp.Columns.Add("Address2");
        dtTemp.Columns.Add("City");
        dtTemp.Columns.Add("State");
        dtTemp.Columns.Add("ZipCode");
        dtTemp.Columns.Add("WebSite");
        dtTemp.Columns.Add("Email");
        dtTemp.Columns.Add("DealAddressID");
        ViewState["AddressDetails"] = dtTemp;
        DataTable dtHighlightTemp = new DataTable();
        dtHighlightTemp.Columns.Add("Highlights");
        dtHighlightTemp.Columns.Add("HighLightID");
        ViewState["Highlight"] = dtHighlightTemp;
        //if (chkTwitter.Checked)
        //{
        //    oAuth.ConsumerKey = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"].ToString();
        //    oAuth.ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"].ToString();
        //    oAuth.Token = System.Configuration.ConfigurationManager.AppSettings["Token"].ToString();
        //    oAuth.TokenSecret = System.Configuration.ConfigurationManager.AppSettings["TokenSecret"].ToString();
        //    string tweet = txtName.Text + "<br/>" + "www.zupons.com/default.aspx?DealID=" + dealID;
        //    string updateurl = "http://twitter.com/statuses/update.xml";
        //    string strXmlResp = oAuth.oAuthWebRequest(oAuthExample.oAuthTwitter.Method.POST, updateurl, "status=" + tweet);
        //}

        Response.Redirect("Deal.aspx");
      
    }
    protected void RadGrid2_UpdateCommand(object source, GridCommandEventArgs e)
    {
        var editItem = ((GridEditableItem)e.Item);
        TextBox txtAddress1 = editItem.FindControl("txtAddress1") as TextBox;
        TextBox txtState = editItem.FindControl("txtState") as TextBox;
        TextBox ZipCode = editItem.FindControl("ZipCode") as TextBox;
        TextBox txtWebSite = editItem.FindControl("txtWebSite") as TextBox;
        TextBox txtEmail = editItem.FindControl("txtEmail") as TextBox;
        TextBox txtAddress2 = editItem.FindControl("txtAddress2") as TextBox;
        TextBox txtCity = editItem.FindControl("txtCity") as TextBox;
        TextBox txtName = e.Item.FindControl("txtName") as TextBox;
        TextBox txtPhoneNo = e.Item.FindControl("txtPhoneNo") as TextBox;
        //   string DealAddressID = (string)((GridDataItem)editItem).GetDataKeyValue("DealAddressID");
        string DealAddressID = (string)editItem.GetDataKeyValue("DealAddressID");
        DataTable dtAddress = new DataTable();
        dtAddress = (DataTable)ViewState["AddressDetails"];
        GridEditableItem editItem1 = (GridEditableItem)((RadGrid)source).NamingContainer;
        RadGrid RadGrid2 = editItem1.FindControl("RadGrid2") as RadGrid;
        for (int i = 0; i < dtAddress.Rows.Count; i++)
        {
            if (dtAddress.Rows[i]["DealAddressID"].ToString() == DealAddressID)
            {
                dtAddress.Rows[i]["Address1"] = txtAddress1.Text;
                dtAddress.Rows[i]["Address2"] = txtAddress2.Text;
                dtAddress.Rows[i]["City"] = txtCity.Text;
                dtAddress.Rows[i]["State"] = txtState.Text;
                dtAddress.Rows[i]["ZipCode"] = ZipCode.Text;
                dtAddress.Rows[i]["WebSite"] = txtWebSite.Text;
                dtAddress.Rows[i]["Email"] = txtEmail.Text;
                dtAddress.Rows[i]["Name"] = txtName.Text;
                dtAddress.Rows[i]["PhoneNo"] = txtPhoneNo.Text;
            }
        }
        ViewState["AddressDetails"] = dtAddress;
        RadGrid2.DataSource = dtAddress;
        RadGrid2.DataBind();
        txtAddress1.Text = "";
        txtAddress2.Text = "";
        txtCity.Text = "";
        txtState.Text = "";
        ZipCode.Text = "";
        txtWebSite.Text = "";
        txtEmail.Text = "";

    }
    protected void RadGrid3_UpdateCommand(object source, GridCommandEventArgs e)
    {
        var editItem = ((GridEditableItem)e.Item);
        TextBox txtHighLights = editItem.FindControl("txtHighLights") as TextBox;

        string HighLightID = (string)editItem.GetDataKeyValue("HighLightID");
        DataTable dtHighLight = new DataTable();
        dtHighLight = (DataTable)ViewState["Highlight"];
        GridEditableItem editItem1 = (GridEditableItem)((RadGrid)source).NamingContainer;
        RadGrid RadGrid3 = editItem1.FindControl("RadGrid3") as RadGrid;
        for (int i = 0; i < dtHighLight.Rows.Count; i++)
        {
            if (dtHighLight.Rows[i]["HighLightID"].ToString() == HighLightID)
            {
                dtHighLight.Rows[i]["Highlights"] = txtHighLights.Text;

            }
        }
        ViewState["Highlight"] = dtHighLight;
        RadGrid3.DataSource = dtHighLight;
        RadGrid3.DataBind();
        txtHighLights.Text = "";


    }
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
       // oAuthExample.oAuthTwitter oAuth = new oAuthExample.oAuthTwitter();
        Label lblError = e.Item.FindControl("lblError") as Label;
        DataTable dtAddress = new DataTable();
        dtAddress = (DataTable)ViewState["AddressDetails"];
       
        DataTable dtHighlight = new DataTable();
        dtHighlight = (DataTable)ViewState["Highlight"];
       
        int updateImg = 0;
        FileUpload FileUpload1 = e.Item.FindControl("GraphicImage") as FileUpload;
        Byte[] bytes = new Byte[10];
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            bytes = br.ReadBytes((Int32)fs.Length);
            updateImg = 0;
        }
        else
        {
            updateImg = 1;

        }
        var editableItem = ((GridEditableItem)e.Item);
        var DealID = (long)editableItem.GetDataKeyValue("DealID");
        var CityID = (long)editableItem.GetDataKeyValue("DealCityID");
        RadComboBox dropCity = e.Item.FindControl("dropCity") as RadComboBox;
        RadComboBox dropVendor = e.Item.FindControl("dropVendor") as RadComboBox;
        RadComboBox dropCategory = e.Item.FindControl("dropCategory") as RadComboBox;
        TextBox txtName = e.Item.FindControl("txtName") as TextBox;
        TextBox txtShortDescription = e.Item.FindControl("txtShortDescription") as TextBox;
        TextBox txtMinQty = e.Item.FindControl("txtMinQty") as TextBox;
        TextBox txtMaxQty = e.Item.FindControl("txtMaxQty") as TextBox;
        TextBox txtPercentageCut = e.Item.FindControl("txtPercentageCut") as TextBox;
        TextBox txtSalesPersonPercentageCut = e.Item.FindControl("txtSalesPersonPercentageCut") as TextBox;
        TextBox txtRegularPrice = e.Item.FindControl("txtRegularPrice") as TextBox;
        TextBox txtSellPrice = e.Item.FindControl("txtSellPrice") as TextBox;
        TextBox txtDiscount = e.Item.FindControl("txtDiscount") as TextBox;
        RadDatePicker RadDatePicker1 = e.Item.FindControl("RadDatePicker1") as RadDatePicker;
        RadDatePicker RadDatePicker2 = e.Item.FindControl("RadDatePicker2") as RadDatePicker;
        RadTimePicker RadTimePicker1 = e.Item.FindControl("RadTimePicker1") as RadTimePicker;
        RadTimePicker RadTimePicker2 = e.Item.FindControl("RadTimePicker2") as RadTimePicker;
        RadDatePicker RadDatePicker3 = e.Item.FindControl("RadDatePicker3") as RadDatePicker;
        RadioButtonList rbtnDealType = e.Item.FindControl("rbtnDealType") as RadioButtonList;
        TextBox txtAddress1 = e.Item.FindControl("txtAddress1") as TextBox;
        TextBox txtAddress2 = e.Item.FindControl("txtAddress2") as TextBox;
        TextBox txtCity = e.Item.FindControl("txtCity") as TextBox;
        TextBox txtState = e.Item.FindControl("txtState") as TextBox;
        TextBox ZipCode = e.Item.FindControl("ZipCode") as TextBox;
        TextBox txtWebSite = e.Item.FindControl("txtWebSite") as TextBox;
        TextBox txtEmail = e.Item.FindControl("txtEmail") as TextBox;
        RadEditor txtFootPrint = e.Item.FindControl("txtFootPrint") as RadEditor;
        RadEditor txtDescription = e.Item.FindControl("txtDescription") as RadEditor;
        TextBox txtLimitPerPerson = e.Item.FindControl("txtLimitPerPerson") as TextBox;   
       // CheckBox chkTwitter = e.Item.FindControl("chkTwitter") as CheckBox;
        string EndTime = Convert.ToDateTime(RadDatePicker2.SelectedDate).ToShortDateString() + " " + RadTimePicker1.SelectedDate.Value.TimeOfDay.ToString();
        string StartTime = Convert.ToDateTime(RadDatePicker1.SelectedDate).ToShortDateString() + " " + RadTimePicker2.SelectedDate.Value.TimeOfDay.ToString();

        if (txtPercentageCut.Text == "")
            txtPercentageCut.Text = "0.0";
        if (txtSalesPersonPercentageCut.Text == "")
            txtSalesPersonPercentageCut.Text = "0.0";
        var deal = new DealesDataContext();
        var query = deal.Update_Deal(Convert.ToInt32(DealID), Convert.ToInt32(dropVendor.SelectedValue), Convert.ToInt32(dropCity.SelectedValue), Convert.ToInt32(dropCategory.SelectedValue), 1, 1, txtName.Text, txtDescription.Content, false, Convert.ToDouble(txtMinQty.Text), Convert.ToDouble(txtMaxQty.Text), 1, bytes, Convert.ToDouble(txtPercentageCut.Text), Convert.ToDouble(txtRegularPrice.Text), Convert.ToDouble(txtSellPrice.Text), Convert.ToDouble(txtDiscount.Text), 1, Convert.ToDouble(txtSalesPersonPercentageCut.Text), Convert.ToDateTime(RadDatePicker1.SelectedDate), Convert.ToDateTime(StartTime), Convert.ToDateTime(RadDatePicker2.SelectedDate), Convert.ToDateTime(EndTime), Convert.ToDateTime(RadDatePicker3.SelectedDate), Convert.ToInt32(rbtnDealType.SelectedValue), txtFootPrint.Content, updateImg, txtShortDescription.Text, Convert.ToInt32(txtLimitPerPerson.Text));

        var DeleteDealAddressID = deal.Delete_DealAddress(Convert.ToInt32(DealID));
        if (dtAddress != null && dtAddress.Rows.Count > 0)
        {
            for (int i = 0; i < dtAddress.Rows.Count; i++)
            {
                var query1 = deal.Insert_DealAddress(dtAddress.Rows[i]["Name"].ToString(),dtAddress.Rows[i]["Address1"].ToString(), dtAddress.Rows[i]["Address2"].ToString(), dtAddress.Rows[i]["City"].ToString(), dtAddress.Rows[i]["State"].ToString(), dtAddress.Rows[i]["ZipCode"].ToString(), dtAddress.Rows[i]["WebSite"].ToString(), dtAddress.Rows[i]["Email"].ToString(),dtAddress.Rows[i]["PhoneNo"].ToString(), Convert.ToInt32(DealID));
            }
        }
        if (dtHighlight != null && dtHighlight.Rows.Count > 0)
        {
            for (int i = 0; i < dtHighlight.Rows.Count; i++)
            {
                var query1 = deal.Insert_HighLights(dtHighlight.Rows[i]["Highlights"].ToString(), Convert.ToInt32(DealID));
            }
        }
        DataTable dtTemp = new DataTable();
        dtTemp.Columns.Add("Address1");
        dtTemp.Columns.Add("Address2");
        dtTemp.Columns.Add("City");
        dtTemp.Columns.Add("State");
        dtTemp.Columns.Add("ZipCode");
        dtTemp.Columns.Add("WebSite");
        dtTemp.Columns.Add("Email");
        dtTemp.Columns.Add("DealAddressID");
        ViewState["AddressDetails"] = dtTemp;
        DataTable dtHighlightTemp = new DataTable();
        dtHighlightTemp.Columns.Add("Highlights");
        dtHighlightTemp.Columns.Add("HighLightID");
        ViewState["Highlight"] = dtHighlightTemp;
        //if (chkTwitter.Checked)
        //{
        //    oAuth.ConsumerKey = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"].ToString();
        //    oAuth.ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"].ToString();
        //    oAuth.Token = System.Configuration.ConfigurationManager.AppSettings["Token"].ToString();
        //    oAuth.TokenSecret = System.Configuration.ConfigurationManager.AppSettings["TokenSecret"].ToString();
        //    string tweet = txtName.Text + "<br/>" + "www.zupons.com/default.aspx?DealID=" + DealID;
        //    string updateurl = "http://twitter.com/statuses/update.xml";
        //    string strXmlResp = oAuth.oAuthWebRequest(oAuthExample.oAuthTwitter.Method.POST, updateurl, "status=" + tweet);
        //}

        Response.Redirect("Deal.aspx");

    }
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "InitInsert")
        {
            e.Item.OwnerTableView.ClearEditItems();
        }
        else if (e.CommandName == "Edit")
        {
            e.Item.OwnerTableView.IsItemInserted = false;
        }
    }
}

